﻿using JStore.Core.Entities;
using SpeadFrm.ORM;

namespace JStore.Data.Daos
{
    /// <summary>
    ///
    /// </summary>
    public class UserPasswordDao : BaseDao<UserPassword>
    {
        public UserPassword GetByUserName(string userName)
        {
            var sql = Sql.Builder.Where("UserName = @0", userName);
            return FirstOrDefault(sql);
        }

        public bool ExistsPower(string logOnUserName, int powerId)
        {
            var sql = Sql.Builder
                .Append("SELECT COUNT(1)")
                .Append("FROM UserPassword a JOIN RolePower b ON a.RoleId = b.RoleId")
                .Append("WHERE a.UserName = @0 AND b.PowerId = @1", logOnUserName, powerId);
            return FirstOrDefault<int>(sql) > 0;
        }

        public bool ExistsUser(string userName)
        {
            var sql = Sql.Builder
                .Select("COUNT(1)")
                .From("UserPassword")
                .Where("UserName = @0", userName);
            return FirstOrDefault<int>(sql) > 0;
        }
    }
}

